package com.mengdie.shuidi.utils.core;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.mengdie.shuidi.AppContext;
import com.mengdie.shuidi.utils.ToastUtil;
import com.mengdie.shuidi.utils.common.RegularUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Created by librabin on 16/10/21.
 */

public class MDAppUtils {

    public static int timenubmer;
    public static long countRemainTime;


    public static void saveObject(String fileName, Object saveObject) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            fos = AppContext.getInstance().openFileOutput(fileName, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fos);
            oos.writeObject(saveObject);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    public static final Object restoreObject(String fileName) {
        FileInputStream fis = null;
        ObjectInputStream ois = null;
        Object object = null;
        try {
            fis = AppContext.getInstance().openFileInput(fileName);
            ois = new ObjectInputStream(fis);
            object = ois.readObject();
            return object;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return object;
    }



    public static boolean checkPhoneNum(String phoneNum) {
        if (TextUtils.isEmpty(phoneNum)) {
            ToastUtil.showToast("手机号不能为空");
            return false;
        }

        if (!RegularUtils.isMobileExact(phoneNum)) {
            ToastUtil.showToast("手机号格式错误!");
            return false;
        }
        return true;
    }

    public static boolean checkPhoneCode(String phoneCode) {
        if (phoneCode.length() == 4) {
            return true;
        } else {

            ToastUtil.showToast("请输入正确的手机验证码");
            return false;
        }
    }

    public static boolean checkPassword(String password) {
        if (password.length() < 6) {
            ToastUtil.showToast("密码不能小于六位");
            return false;
        }

        return true;

    }

    public static void installAPK(Context context, File file) {
        if (file == null || !file.exists())
            return;
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        context.startActivity(intent);
    }

}
